
/**
 * This holds the main library entrance points and other functions that
 * stand on their own.
 */

#include <swarm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

sw_swarm *internal_sw;

void sw_print_lib_info(){

  printf("Swarm Library: version=%s\n", _SWARM_VERSION);
  printf(" CPU: %s\n", _SWARM_PROC);
  printf(" Arch: %s | Processors: %d\n", _SWARM_ARCH, _SWARM_PROCS);
  printf(" Instruction sets: ");
#ifdef _SWARM_HAVE_MMX
  printf("MMX ");
#endif
#ifdef _SWARM_HAVE_SSE
  printf("SSE ");
#endif
#ifdef _SWARM_HAVE_SSE2
  printf("SSE2 ");
#endif
#ifdef _SWARM_HAVE_SSE3
  printf("SSE3 ");
#endif
#ifdef _SWARM_HAVE_3Dnow
  printf("3Dnow ");
#endif
  printf("\n");

}

void sw_exit(int status){

  printf("[swarm] Exit: %d\n", status);
  exit(status);

}

void sw_print_swarm_info(sw_swarm *sw){

  printf("[swarm] Swarm name: %s\n", sw->name);
  printf("[swarm]   Window size: ");
  sw_println_vec(&sw->size);
  printf("[swarm]   Tau:        %lf\n", sw->tau);
  printf("[swarm]   Threads:    %d\n", sw->threads);
  printf("[swarm]   Entropy:    %lf\n", sw->entropy);
  printf("[swarm]   Sim length: %lf\n", sw->sim_len);

}

sw_swarm *sw_swarm_new(){

  sw_swarm *sw = (sw_swarm *) malloc(sizeof(sw_swarm));
  memset(sw, 0, sizeof(sw_swarm));
  return sw;

}

int sw_init(int *argc, char ***argv){

  /* First load the default signal handlers. */
  sw_init_signals();
  sw_init_rand(0, 100); // Default rand min and max

  /* Do any arg parsing that should happen. */
  // Coming later

  return 0;

}

void _sw_set_internal(sw_swarm *sw){

  internal_sw = sw;

}

void sw_print_force_table(sw_swarm *sw){

  printf("[swarm] Force table:\n");

  int i = 0;
  for ( ; i < sw->f_len; i++){
    sw_print_force(&sw->forces[i]);
  }

}

void _sw_increase_swarm_space(sw_swarm *sw){

  sw_insect **swarm = (sw_insect **)malloc((sw->swarm_space + 32) * 
					   sizeof(sw_insect *));

  memset(swarm, 0, (sw->swarm_space + 32) * sizeof(sw_insect *));
  memcpy(swarm, sw->swarm, sw->swarm_size * sizeof(sw_insect *));

  if (sw->swarm != NULL) free(sw->swarm);
  sw->swarm = swarm;
  sw->swarm_space += 32;

}

void sw_add_insect(sw_swarm *sw, sw_insect *ins){

  if (sw->swarm_space <= sw->swarm_size){
    _sw_increase_swarm_space(sw);
  }

  sw->swarm[sw->swarm_size++] = ins;

}


